package com.fyt.ZhongYaoApp.service.impl;

import com.fyt.ZhongYaoApp.mapper.ShoppingCarMapper;
import com.fyt.ZhongYaoApp.mapper.ZYMapper;
import com.fyt.ZhongYaoApp.pojo.ShoppingCar;
import com.fyt.ZhongYaoApp.pojo.User;
import com.fyt.ZhongYaoApp.pojo.vo.ZYQuerry;
import com.fyt.ZhongYaoApp.pojo.dto.ShoppingCarDTO;
import com.fyt.ZhongYaoApp.service.ShoppingCarService;
import com.fyt.ZhongYaoApp.service.UserService;
import com.fyt.ZhongYaoApp.utils.ThreadLocalUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class ShoppingCarServiceImpl implements ShoppingCarService {

    @Autowired
    private ShoppingCarMapper shoppingCarMapper;

    @Autowired
    private ZYMapper zyMapper;

    @Autowired
    private UserService userService;

    // 添加购物车
    @Override
    public void addCar(ShoppingCarDTO shoppingCarDTO) {
        // 判断当前加入购物车中的商品是否已经存在
        ShoppingCar shoppingCar = new ShoppingCar();
        BeanUtils.copyProperties(shoppingCarDTO, shoppingCar);// 数据拷贝
        // 从头请求得到用户id
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String)map.get("username");
        User user = userService.findByUserName(username);
        shoppingCar.setUserId(user.getId());

        System.out.println(shoppingCar);

        List<ShoppingCar> list = shoppingCarMapper.list(shoppingCar);

        System.out.println(list);

        // 如果存在，只需要数量+1
        if(list.size()>0 && list != null){
            ShoppingCar car = list.get(0);// 得到数量

            // 查找商品价格
            Integer shopId = shoppingCar.getShopId();
            ZYQuerry zyQuerry = zyMapper.zyFindById(shopId);

            System.out.println("添加前的数量：" + car.getNumber());
            System.out.println("添加前的价格：" + car.getAmount());
            // 修改购物车数量
            car.setNumber(car.getNumber()+1);// update shopping_car set number = ? where id = ?
            // 修改商品总价
            car.setAmount(zyQuerry.getAmount() * car.getNumber());

            System.out.println("添加后的数量：" + car.getNumber());
            System.out.println("添加后的价格" + car.getAmount());
            System.out.println(car);
            shoppingCarMapper.updateNumberById(car);
        }else {
            // 如果不存在，需要插入一条购物车数据
            Integer shopId = shoppingCar.getShopId();
            System.out.println("id = " + shopId);
            ZYQuerry zyQuerry = zyMapper.zyFindById(shopId);
            System.out.println("list =" + zyQuerry);
            shoppingCar.setName(zyQuerry.getName());
            shoppingCar.setImage(zyQuerry.getZyPic());
            shoppingCar.setAmount(zyQuerry.getAmount());
            shoppingCar.setNumber(1);
            shoppingCar.setCreateTime(LocalDateTime.now());
            shoppingCar.setShopCat(zyQuerry.getCatName());
            shoppingCarMapper.insert(shoppingCar);


        }


    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCar> showShoppingCar() {

        ShoppingCar shoppingCar = new ShoppingCar();
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String)map.get("username");
        User user = userService.findByUserName(username);
        shoppingCar.setUserId(user.getId());

        System.out.println(shoppingCar);

        List<ShoppingCar> list = shoppingCarMapper.list(shoppingCar);

        System.out.println(list);

        return list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCar() {
//        ShoppingCar shoppingCar = new ShoppingCar();
        Integer userId;
        Map<String,Object> map = ThreadLocalUtil.get();
        String username = (String)map.get("username");
        User user = userService.findByUserName(username);
        userId = user.getId();
        shoppingCarMapper.deleteById(userId);
    }
}
